package com.demo.auth.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * 请求工具类
 */
public class RequestUtils {

    /**
     * 获取登录认证的客户端类型
     * 放在请求头（Request Headers）中的client_type字段，且经过加密，例如 client_type Y2xpZW50OnNlY3JldA== 明文等于 client_type:ADMIN
     *
     * @return 客户端类型
     */
    public static String getOAuth2ClientType() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        // 从请求头获取
        String clientType = request.getHeader("client_type");
        return new String(Base64.getDecoder().decode(clientType.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
    }
}
